package com.devin.avatar.common.entity;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;

import com.devin.avatar.common.util.UUIDUtil;

public abstract class DataEntity extends BaseEntity {
    private static final long serialVersionUID = 1L;

    protected String id; // 实体编号（唯一标识）
    protected Date createDate; // 创建日期
    protected String createBy;
    protected Date updateDate; // 更新日期
    protected String updateBy;
    protected String delFlag; // 删除标记（0：正常；1：删除；2：审核）

    /**
     * ID是否自动生成，false需从手动传入。
     */
    protected boolean isAutoID = true;

    public DataEntity() {
	super();
	this.delFlag = DEL_FLAG_NORMAL;
    }

    public String getId() {
	return id;
    }

    public void setId(String id) {
	this.id = id;
    }

    public Date getCreateDate() {
	return createDate;
    }

    public void setCreateDate(Date createDate) {
	this.createDate = createDate;
    }

    public String getCreateBy() {
	return createBy;
    }

    public void setCreateBy(String createBy) {
	this.createBy = createBy;
    }

    public Date getUpdateDate() {
	return updateDate;
    }

    public void setUpdateDate(Date updateDate) {
	this.updateDate = updateDate;
    }

    public String getUpdateBy() {
	return updateBy;
    }

    public void setUpdateBy(String updateBy) {
	this.updateBy = updateBy;
    }

    public String getDelFlag() {
	return delFlag;
    }

    public void setDelFlag(String delFlag) {
	this.delFlag = delFlag;
    }

    public boolean isAutoID() {
	return isAutoID;
    }

    public void setAutoID(boolean isAutoID) {
	this.isAutoID = isAutoID;
    }

    @Override
    public void preInsert() {
	// 调用setAutoID(false), 使用自定义ID
	if (!this.isAutoID() && StringUtils.isBlank(this.getId())) {
	    this.setId(UUIDUtil.randomUUID());
	}
	// TODO createBy & updateBy
	this.updateDate = new Date();
	this.createDate = this.updateDate;
    }

    /**
     * 更新之前执行方法，需要手动调用
     */
    @Override
    public void preUpdate() {
	// TODO updateBy
	this.updateDate = new Date();
    }
}
